package com.diao.myhub.controller;

import com.diao.myhub.exception.CustomizeError;
import com.diao.myhub.exception.CustomizeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import static com.diao.myhub.util.FileUtils.uploadToServer;

@Controller
@Slf4j
@RestController
public class FileController {

    @Value("${upload.filepath}")
    private String filepath;

    @RequestMapping("/file/img-upload")
    public String imgUpload(@RequestParam("file") MultipartFile file) {
        String url = uploadToServer(file, filepath);
        if (!StringUtils.hasText(url)) {
            throw new CustomizeException(CustomizeError.FILE_UPLOAD_ERROR);
        }
        return url;
    }
}
